//
//  XQMeCell.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/2/4.
//

import UIKit

class XQMeCell: UITableViewCell {
    
    private lazy var logoImageView : UIImageView = {
        let imageView = UIImageView()
        return imageView
    }()
    
    private lazy var titleLb : UILabel = {
       let lb = UILabel()
        lb.textColor = .xq_black
        lb.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        return lb
    }()
    
    private lazy var arrowImageView : UIImageView = {
       let arrow = UIImageView()
        arrow.image = UIImage(named: "me_arrow")
        arrow.sizeToFit()
        return arrow;
    }()
    
    private lazy var lineView : UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(hex: "#EFEFEF")
        return v
    }()
    
    var model : XQMeReadingsModel? {
        didSet {
            if let url = URL(string: model?.repeatable ?? "") {
                self.logoImageView.kf.setImage(with: url)
            }
            self.titleLb.text = model?.accessing
        }
    }
    
    var isLast : Bool = false {
        didSet {
            self.lineView.isHidden = isLast
        }
    }

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
       super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        makeUI()
        makeConstraints()
    }

    required init?(coder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
    }
    
    private func makeUI() {
        self.contentView.addSubview(self.logoImageView)
        self.contentView.addSubview(self.titleLb)
        self.contentView.addSubview(self.arrowImageView)
        self.contentView.addSubview(self.lineView)
    }
    
    private func makeConstraints() {
        self.logoImageView.snp.makeConstraints { make in
            make.top.equalTo(14.5)
            make.left.equalTo(24)
            make.bottom.equalTo(-14.5)
            make.width.height.equalTo(50)
        }
        self.titleLb.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(self.logoImageView.snp.right).offset(16)
        }
        self.arrowImageView.snp.makeConstraints { make in
            make.right.equalTo(-24)
            make.centerY.equalToSuperview()
        }
        self.lineView.snp.makeConstraints { make in
            make.left.equalTo(24)
            make.right.equalTo(-24)
            make.bottom.equalTo(0)
            make.height.equalTo(1)
        }
    }
    
}
